package com.controller;

import com.ReturnDTO.ReturnDTO;
import com.entity.Student;
import com.entity.User;
import com.service.StudentService;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 学生信息控制层
 */
@Controller
@RequestMapping(value = "/student")
public class StudentControlle {

  @Autowired
  private StudentService studentService;

  /**
   * 内部返回视图
   * @param modelAndView
   * @return
   */
  @RequestMapping(value = "/toStudentPage")
  public ModelAndView toStudentPage(ModelAndView modelAndView) {
    modelAndView.setViewName("/student");
    return modelAndView;
  }

  @RequestMapping(value = "/toAddStudentPage")
  public ModelAndView toAddStudentPage(ModelAndView modelAndView) {
    modelAndView.setViewName("/addStudent");
    return modelAndView;
  }

  @RequestMapping(value = "/toSelectAloneStudent")
  public ModelAndView toSelectAloneStudent(ModelAndView modelAndView) {
    modelAndView.setViewName("/selectAloneStudent");
    return modelAndView;
  }

  /**
   * 添加学生信息（200，成功），返回页面，
   * @param student
   * @param modelAndView
   * @param session
   * @return
   */
  @RequestMapping(value = "/addStudent")
  public ModelAndView addStudent(Student student, ModelAndView modelAndView, HttpSession session) {
    User user = (User) session.getAttribute("User");
    if (user == null) {
      ReturnDTO returnDTO = ReturnDTO.getReturnDTO(500, "会话超时");
      modelAndView.setViewName("/login");
      modelAndView.addObject("msg", returnDTO.getMsg());
    }
    ReturnDTO returnDTO = studentService.addStudent(student);
    if (returnDTO.getCode() != 200) {
      modelAndView.addObject("returnDTO",returnDTO);
      modelAndView.setViewName("/addStudent");
      return modelAndView;
    }
    modelAndView.setViewName("/student");
    return modelAndView;
  }

  /**
   * 查询学生信息
   * @param student
   * @return
   */
  @RequestMapping(value = "/selectStudent")
  @ResponseBody
  public ReturnDTO selectStudent(Student student){
    ReturnDTO returnDTO = studentService.selectStudent(student);
    return returnDTO;
  }

  /**
   * 查询一条学生信息返回页面
   * @param student
   * @param modelAndView
   * @return
   */
  @RequestMapping(value = "/selectAloneStudent")
  public ModelAndView selectAloneStudent(Student student,ModelAndView modelAndView) {
    ReturnDTO returnDTO = studentService.selectAloneStudent(student);
    if (returnDTO.getCode() == 200){
      Student fromStudent = (Student) returnDTO.getData();
      modelAndView.addObject("Student",fromStudent);
      modelAndView.setViewName("/selectAloneStudent");
    }
    return modelAndView;
  }

  /**
   * 修改学生信息
   * @param student
   * @param modelAndView
   * @return
   */
  @RequestMapping(value = "/updateStudent")
  public ModelAndView updateStudent(Student student,ModelAndView modelAndView){
    ReturnDTO returnDTO = studentService.updateStudent(student);
    if (returnDTO.getCode() == 200){
        modelAndView.addObject("returnDTO",returnDTO);
        modelAndView.setViewName("/student");
    }
    return modelAndView;
  }

  /**
   * 删除学生信息
   * @param student
   * @param modelAndView
   * @return
   */
  @RequestMapping(value = "/deleteStudent")
  public ModelAndView deleteStudent(Student student,ModelAndView modelAndView){
    ReturnDTO returnDTO = studentService.deleteStudent(student);
    if (returnDTO.getCode() != 200){
      modelAndView.addObject("returnDTO",returnDTO);
      modelAndView.setViewName("/student");
    }
    modelAndView.setViewName("/student");
    return modelAndView;
  }
}
